﻿using System.ComponentModel;
using System.Windows;
using Gavel.Common.Utilities.Const;
using Gavel.Common.Utilities.Extension;
using Gavel.ModLink.Model;

namespace Gavel.ModLink.ViewModel;

/// <summary>
/// 寄存器值写入视图模型
/// </summary>
public class RegisterWriteViewModel : INotifyPropertyChanged
{
    /// <summary>
    /// 属性修改事件
    /// </summary>
    public event PropertyChangedEventHandler? PropertyChanged;

    /// <summary>
    /// 视图可见
    /// </summary>
    private Visibility _showView = Visibility.Collapsed;

    /// <summary>
    /// 视图可见
    /// </summary>
    public Visibility ShowView
    {
        get => _showView;
        set
        {
            _showView = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ShowView)));
        }
    }
    
    /// <summary>
    /// 从机地址
    /// </summary>
    public byte Slave { get; set; }
    
    /// <summary>
    /// 协议类型
    /// </summary>
    public ProtocolType Protocol { get; set; }
    
    /// <summary>
    /// 寄存器十进制地址
    /// </summary>
    private ushort _addressDecimal;

    /// <summary>
    /// 寄存器十进制地址
    /// </summary>
    public ushort AddressDecimal
    {
        get => _addressDecimal;
        set
        {
            _addressDecimal = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AddressDecimal)));
        }
    }

    /// <summary>
    /// 寄存器十六进制地址
    /// </summary>
    public string AddressHexadecimal
    {
        get => AddressDecimal.ToString("X4");
        set => _ = value;
    }

    /// <summary>
    /// 寄存器十进制值
    /// </summary>
    private ushort _decimal;

    /// <summary>
    /// 寄存器十进制值
    /// </summary>
    public ushort Decimal
    {
        get => _decimal;
        set
        {
            _decimal = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Decimal)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Hexadecimal)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Binary)));
        }
    }

    /// <summary>
    /// 寄存器十六进制值
    /// </summary>
    public string Hexadecimal
    {
        get => Decimal.ToBytes().ToHex();
        set => _ = value;
    }

    /// <summary>
    /// 寄存器二进制值
    /// </summary>
    public string Binary
    {
        get => Decimal.ToBytes().ToBinary();
        set => _ = value;
    }

    /// <summary>
    /// 功能码
    /// </summary>
    private Pair<FunctionType, string> _function = new(FunctionType.WriteSingle, "06 预置单个寄存器");

    /// <summary>
    /// 功能码
    /// </summary>
    public Pair<FunctionType, string> Function
    {
        get => _function;
        set
        {
            _function = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Function)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(RegisterCountEnabled)));
        }
    }

    /// <summary>
    /// 功能码列表
    /// </summary>
    public static List<Pair<FunctionType, string>> FunctionList =>
    [
        new(FunctionType.WriteSingle, "06 预置单个寄存器"),
        new(FunctionType.WriteMultiple, "10 预置多个寄存器")
    ];

    /// <summary>
    /// 启用预置数量
    /// </summary>
    public bool RegisterCountEnabled => Function.Key == FunctionType.WriteMultiple;

    /// <summary>
    /// 预置值
    /// </summary>
    private ushort _registerValue;

    /// <summary>
    /// 预置值
    /// </summary>
    public ushort RegisterValue
    {
        get => _registerValue;
        set
        {
            _registerValue = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(RegisterValue)));
        }
    }

    /// <summary>
    /// 大小端
    /// </summary>
    public Endian Endian { get; set; }
}